package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.cultivate.entity.Dict;
import com.xmy.cultivate.service.IDictService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/dict")
public class DictController extends BaseController{

    @Autowired
    IDictService iDictService;


    @GetMapping("/findForTypes")
    public Result findForTypes(@RequestParam Map reMap){
        try{
            String type = (String) reMap.get("type");
            QueryWrapper<Dict> dictQueryWrapper =  new QueryWrapper<>();
            dictQueryWrapper.eq("deleted",0);
            dictQueryWrapper.eq(StringUtils.isNotBlank(type),"type",type);
            dictQueryWrapper.eq("status",1);
            dictQueryWrapper.orderByAsc("num");
            List<Dict> dictList = iDictService.list(dictQueryWrapper);
            for (Dict dict:dictList){
                dict.setId(Long.parseLong(dict.getNum().toString()));
            }
            return new Result(Code.OK,new PageResult(dictList,(long)dictList.size(),1L));

        }catch (Exception e){
            return new Result(Code.ERROR,e.getMessage());
        }
    }

}
